//
//  NYMyStarRankingListViewController.m
//  NiuWaJiaoYu
//
//  Created by MinLison on 2018/1/18.
//  Copyright © 2018年 niuwaketang. All rights reserved.
//

#import "NYMyStarRankingListViewController.h"
#import "NYMyStarRankingRequest.h"
#import "NYRefreshFooter.h"
#import "NYMyStarRankingHeaderView.h"
@interface NYMyStarRankingListViewController ()
@property(nonatomic, strong) NYMyStarRankingRequest *request;
@property(nonatomic, strong) NYMyRankingInfoModel *userInfo;
@end

@implementation NYMyStarRankingListViewController
- (instancetype)initWithStyle:(UITableViewStyle)style
{
        return [super initWithStyle:UITableViewStyleGrouped];
}
- (void)viewDidLoad
{
        [super viewDidLoad];
        [self loadData];
}
- (void)loadData
{
        [self configNimbus];
        [self setLoading:YES animation:YES];
        self.request = [NYMyStarRankingRequest requestWithParams:@{
                                                                   @"genre" : self.genre
                                                                   }];
        [[self.request pullDown] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYMyStarRankingModel * _Nonnull data) {
                self.userInfo = data.userInfo;
                [self configNimbusDataForMutableSections:data];
                [self setSuccess];
        } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                [self setError:error];
        }];
}
- (void)configNimbus
{
        [super configNimbus];
        [self.tableView registerClass:[NYMyStarRankingHeaderView class] forHeaderFooterViewReuseIdentifier:[NYMyStarRankingHeaderView className]];
}
- (void)configNimbusDataForMutableSections:(NYMyStarRankingModel *)data
{
        [self.tableViewModel addObjectsFromArray:data.ranking];
        [self.tableView reloadData];
}
- (void)setSuccess
{
        [super setSuccess];
        [self configRefreshFooter];
}
/// MARK: - TableView
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
        if (section == 0) {
                return UITableViewAutomaticDimension;
        }
        if (@available(iOS 11, *)) {
                return CGFLOAT_MIN;
        }
        return 0.0001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
        NYMyStarRankingHeaderView *headerView = (NYMyStarRankingHeaderView *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:[NYMyStarRankingHeaderView className]];
        headerView.infoModel = self.userInfo;
        return headerView;
}
/// MARK: - Private
- (void)configRefreshFooter
{
        @weakify(self);
        self.tableView.mj_footer = [NYRefreshFooter footerWithRefreshingBlock:^{
                @strongify(self);
                [[self.request pullUp] startWithSuccess:^(__kindof BaseRequest * _Nonnull request, __kindof NYMyStarRankingModel * _Nonnull data) {
                        [self configNimbusDataForMutableSections:data];
                        data.ranking.count == 0 ? [self.tableView.mj_footer endRefreshingWithNoMoreData] : [self.tableView.mj_footer endRefreshing];
                } failed:^(__kindof BaseRequest * _Nonnull request, NSError * _Nonnull error) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }];
        }];
}
+ (NYViewControllerNavigationBarType)navigationBarType
{
        return NYViewControllerNavigationBarTypeBlue;
}

@end
